const app = getApp()
Component({
    mixins: [],
    data: {
        
    },
    props: {
        onLgin:(data) => data,
        show:true
    },
  didMount() {},
  didUpdate() {},
  didUnmount() {},
  methods: {
        closemodalOpened(){
            let obj = {
                loginShow:false,
                user_header: '',
                user_name: '',
                login:false
            }
            this.props.onLgin(obj);
        },
        onSubmit(){
            console.log("1111111111111")
            my.showLoading({content: '登录中...',});
            my.getOpenUserInfo({
                fail: (res) => {
                    my.alert({ content: "请先登录后使用" });
                },
                success: (res) => {
                    console.log("getOpenUserInfo=================>>>>>>", res.response);
                    this.login(res.response)
                    .then((res)=>{
                        let obj = {
                            loginShow:false,
                            user_header: res.data.user.header,
                            user_name: res.data.user.uname,
                            login:true
                        }
                        
                        this.props.onLgin(obj);
                    })
                    .catch((err)=>{
                        my.hideLoading();
                    })
                }
            });
           
        },

        // // 登录
        login(response, func) {
            
            console.log("======================login登录=========================")
            return new Promise((resolve,reject)=>{
                const that = this;
                func = func ? func : () => { };
                response = response ? response : "";
                if (!response) {return}
                my.getAuthCode({
                    scopes: 'auth_base',
                    success: (res) => {
                        let promoteId = app.globalData.query.promoteId;
                        let promoteUserId = app.globalData.query.promoteUserId;
                        // console.log("getAuthCode===========>>>", res, response)
                        let data = {
                            authCode: res.authCode,
                            param: response,
                            // promotionAccount: app.globalData.promotionAccount,
                            // flowState: app.globalData.flowState
                        }
                        if(promoteId){
                          data.promoteId = promoteId;
                        }
                        if(promoteUserId){
                          data.promoteUserId = promoteUserId;
                        }

                        app.user.login(data)
                        .then(json=>{
                            console.log("===============>>>登陆",json)
                            if (json.code != 200) { my.alert({ content: json.msg }); return; }
                            json.data.user.promoteId = json.data.promoteId;
                            json.data.user.promoteUserId = json.data.promoteUserId;
                            my.setStorage({
                                key: 'user',
                                data: json.data.user,
                            })
                            my.setStorage({
                                key: 'token',
                                data: json.data.token,
                            })
                            console.log(123456789,json.data)
                            resolve(json);
                            my.hideLoading();
                        })
                        .catch((err)=>{
                            my.hideLoading();
                        })
                        
                    },
                    fail: () => {
                        reject();
                        my.hideLoading();
                    }
                });
                
            })
            
        },
        
  },
});
